<?php
class test_xdal  extends PHPUnit_TestCase {

    function __construct($name) {
        $this->PHPUnit_TestCase($name);
    }

    function test_childs() {
        $q = "CALL childs('relations', 'user', '1', '%', 'test_childs')";
        $c = "SELECT COUNT(*) as count FROM test_childs";
        $this->call_test($q, $c);
    }

    function test_init() {
        $q = "CALL init(md5('admin'), md5('admin'), '')";
        $c = "SELECT * FROM self";
        $this->call_test($q, $c);
    }

    function call_test($q, $c = false) {
        $res = mysqli_query($_SESSION['pconnect'], $q) or $this->fail(mysqli_error($_SESSION['pconnect']));
        if ($c) {
            $res =  mysqli_query($_SESSION['pconnect'], $c) or $this->fail(mysqli_error($_SESSION['pconnect']));
        }
        $this->assertTrue(mysqli_fetch_field($res), "Query returned empty resultset");
        $this->assertTrue((mysqli_affected_rows($_SESSION['pconnect']) > 0), "No rows affected by query...");
    }

    function test_branch() {
        $query='CALL create_result_table("result")';
        $test="INSERT INTO `__RQL_QUERY` VALUES ".
                "(0, 0, 1, 'group', '.', '')";
        $this->call_test($query, $test);
        $query = "CALL branch('self', '__QUERY_INITIAL', 'result', 0)";
        $test = "SELECT * FROM `result`";
        $this->call_test($query, $test);
    }
}
?>